<?php
namespace app\admin\controller;
use app\admin\model\NodeModel;
use think\Db;
class Index extends Base
{
    public function index(){
        // 获取权限菜单
        $node = new NodeModel();
        $this->assign([
            'menu' => $node->getMenu(session('rule'))
        ]);
        return $this->fetch('/index');
    }

    /**
     * [后台默认首页]
     */
    public function indexPage(){
		$this->getUnSubTable();
        return $this->fetch('index');
    }

	/**
	 * [获取登陆账号所属地区的未提交报表]
	 */
	public function getUnSubTable() {
		$tableArr = ['month','year','capital','asset','day'];
		$department = DB::name('department')->alias('depart')
			->join('role role','depart.id = role.department_id')
			->join('user user','user.role_id = role.id')
			->where('user.id',session('id'))
			->field('depart.name,depart.id')->find();
		$unSubTable = [];
		foreach($tableArr as $k => $v){
			$tableName = "table_".$v;
			$res = DB::name($tableName)->where([
				'is_commit' => 0,
				'depart_id' => $department['id']
			])->select();
			foreach($res as $va){
				$timeFormat = "Y年m月d日";
				switch ($v) {
					case 'month':
						$timeFormat = "Y年m月";
						$va['table_type'] = '月报';break;
					case 'year':
						$timeFormat = "Y年";
						$va['table_type'] = '年报';break;
					case 'capital':
						$va['table_type'] = '资金管理:企业-'.$va['enter_name'];break;
					case 'asset':
						$timeFormat = "Y年m月";
						$va['table_type'] = '固定资产表';break;
					case 'day':
						$timeFormat = "Y年m月d日";
						$va['table_type'] = '企业日报表';break;
					default:
						$va['table_type'] = '';break;
						break;
				}
				if(isset($va['rpt_date'])){
					$va['rpt_date'] = date($timeFormat,$va['rpt_date']);
				}
				$unSubTable[] = $va;
			}
		}
		if(empty($unSubTable)){
			$unSubTable[] = [
				'table_type' => '没有'
			];
		}
		$this->assign([
			'unSubTable' => $unSubTable
		]);
	}

	/**
	 * [首页统计信息表格]
	 */
	public function getStatisticTable() {
		$depart = DB::name('department')->alias('depart')
			->join('role role','depart.id = role.department_id')
			->join('user user','user.role_id = role.id')
			->where('user.id',session('id'))
			->field('depart.name,depart.id')->find();
		$where = ['depart_id' => $depart['id']];
		# tableData1
		$tableData1['a'] = DB::name("statistics_recfp")->where($where)->sum("cover_0");
		$tableData1['b'] = DB::name("statistics_recfp")->where($where)->sum("cover_1");
		$tableData1['c'] = DB::name("statistics_recfp")->where($where)->sum("train_1");
		$tableData1['d'] = DB::name("statistics_recfp")->where($where)->sum("train_3");
		$data['tableData1'][] = $tableData1;
		# tableData2
		$tableData2['a'] = DB::name("statistics_pubservice")->where($where)->sum("county");
		$tableData2['b'] = DB::name("statistics_pubservice")->where($where)->sum("town");
		$tableData2['c'] = DB::name("statistics_pubservice")->where($where)->sum("country");
		$data['tableData2'][] = $tableData2;
		# tableData3
		$tableData3['a'] = DB::name("statistics_logistics")->where($where)->sum("county_num");
		$tableData3['b'] = DB::name("statistics_logistics")->where($where)->sum("town_num");
		$data['tableData3'][] = $tableData3;
		# tableData4
		$tableData4['a'] = DB::name("statistics_rectrain")->where($where)->sum("train_sum");
		$tableData4['b'] = DB::name("statistics_rectrain")->where($where)->sum("drive_job");
		$data['tableData4'][] = $tableData4;
		# tableData5
		$tableData5['a'] = DB::name("statistics_capitaluse")->where($where)->sum("center_pay");
		$center_money = DB::name("statistics_capitaluse")->where($where)->sum("center_money");
		if($center_money){
			$tableData5['b'] = round(($tableData5['a'] / $center_money * 100),2);
		}else{
			$tableData5['b'] = 0;
		}
		$data['tableData5'][] = $tableData5;
		# tableData6
		$tableData6['a'] = DB::name("statistics_capitalmgr")->where($where)->sum("already");
		$tableData6['b'] = DB::name("statistics_capitalmgr")->where($where)->sum("unalready");
		$data['tableData6'][] = $tableData6;
		return ['code'=>1,'msg'=>'success','data'=>$data,'depart'=>$depart];
	}

	/**
	 * [请求接口测试]
	 */
	public function askAPI_test() {
		$data = [
		    'userId' => '5329300001',
		    'rptDate' => '2018-10-16',
		    'serviceStationReport' => [
	            'code' => '124',
	            'name' => '洱源乡土公社文化传播有限公司',
	            'countyType' => '2',
	            'buyOrder' => '24',
	            'saleOrder' => '24',
	            'serviceStationCommodity' => [
                    'commId' => '23',
                    'money' => '123',
                ],
	        ]
		];
		$data = $this->Arr2XML($data,'serviceStation');
		$res = $this->sendXML2API($data);
		print_r($res);die;
	}
}
